package my.sample.quartz;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.CronScheduleBuilder.cronSchedule;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

public class MyJob implements Job {

	/**
	 * @param args
	 * @throws SchedulerException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws SchedulerException, InterruptedException {
		// TODO Auto-generated method stub
		
		Logger log = LoggerFactory.getLogger(MyJob.class);
		
		SchedulerFactory fact = new StdSchedulerFactory();
		Scheduler scheduler = fact.getScheduler();
		System.out.println("scheduler name: " + scheduler.getSchedulerName());
		scheduler.start();
		
		// job
		JobDetail job = newJob(MyJob.class).withIdentity("myFirstJob", "myGroup").build();
//		JobDetail job1 = newJob(MyJob.class).withIdentity("mySecondJob", "myGroup").build();
		
		
		Date start = new Date();
		start.setYear(113);
		start.setMonth(9);
		start.setDate(11);
		start.setHours(17);
		start.setMinutes(58);
		start.setSeconds(0);
		
		Date stop = new Date();
		stop.setYear(113);
		stop.setMonth(9);
		stop.setDate(11);
		stop.setHours(17);
		stop.setMinutes(59);
		stop.setSeconds(0);
		
		
		
		System.out.println(start);
		System.out.println(stop);
		Date now = new Date();
		System.out.println(now);
		
		System.out.println(now.getYear());
		System.out.println(now.getMonth());
		System.out.println(now.getDate());
		System.out.println(now.getHours());
		System.out.println(now.getMinutes());
		System.out.println(now.getSeconds());
				
		
		
		// trigger
		CronTrigger trigger = newTrigger()
				.withIdentity("trigger", "triggerGroup")
				.startAt(start)
				.endAt(stop)
				.withSchedule(cronSchedule("0/10 * * * * ?")).build();
				
		
//        CronTrigger trigger1 = newTrigger().withIdentity("secondTrigger", "triggerGroup").withSchedule(cronSchedule("0/5 * * * * ?")).build();
		
		//trigger own
		
        
		scheduler.scheduleJob(job, trigger);
//		scheduler.scheduleJob(job, trigger1);
//		scheduler.scheduleJob(
		
//		Thread.sleep(25000);
//		scheduler.deleteJob(job.getKey());
//		scheduler.pauseJob(job.getKey());
//		Thread.sleep(10000);
//		scheduler.resumeJob(job.getKey());
		
		log.info("sch started");
	}

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			
			String fileName = context.getJobDetail().getKey().getName();
			File file = new File(fileName + ".txt");

			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}

			// true = append file
			FileWriter fileWritter = new FileWriter(file.getName(), true);
			BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
			bufferWritter.write(new Date().toString() + "\n");
			bufferWritter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
